package com.example.ws.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ws.mapper.UserMapper;
import com.example.ws.model.User;
import com.example.ws.service.IUserService;
import com.example.ws.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务业务逻辑
 * @Service 表示服务类
 *
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

//添加用户
    @Override
    public String add(User user) {
        if (user ==null ){
            throw new RuntimeException("用户实体不能为空");
        }
        int insert = userMapper.insert(user);
        if (insert>0){
            return user.getName();
        }
        throw new RuntimeException("新增失败");
    }
//删除用户
    @Override
    public int delete(String userId) {
        int delete = userMapper.deleteById(userId);
        if (delete>0){
            return delete;
        }
        throw new RuntimeException("删除失败！ id："+userId);
    }
//编辑用户信息
    @Override
    public User editByUserId(User user) {
        if (user == null){
            throw new RuntimeException("用户信息为空，更新失败");
        }
           int update = userMapper.updateById(user);
        if (update >0){
            return user;
        }
        throw new RuntimeException("更新用户失败！");
    }
//获取用户详情
    @Override
    public User detail(String userId) {
        //验证用户id不能为空字符
        if (StrUtil.isBlank(userId)){
            throw new RuntimeException("用户id不能为空");
        }
        return userMapper.selectById(userId);
    }

    @Override
    public User login(String name, String password) {
        if (name == null && password == null){
            throw new RuntimeException("用户名，密码不能为空！");
        }
        User log = userMapper.selectAllCount(name,password);
        if (log !=null){
            return log;
        }
        throw new RuntimeException("查询用户失败！");
    }

    //分页查询用户列表
    @Override
    public PageVo page(String userName, int pageNumber, int pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        /**
         * 构建查询条件
         * 判断userName是否不为空且不为空白字符
         * 添加一个模糊查询，查询name字段中包含userName的记录
         */
        if (StrUtil.isNotBlank(userName)){
            queryWrapper.like("name",userName);
        }
        /**
         * 构建分页查询参数
         * 调用userMapper的selectPage方法查询
         */
        IPage<User> pageParam = new Page<>(pageNumber,pageSize);
        IPage<User> userIPage = userMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(userIPage);//结果封装为PageVo对象
    }

    @Override
    public List<User> listEditByUserId(List<String> useridList) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",useridList);
        return userMapper.selectList(queryWrapper);
    }
}
